﻿Public Class MoneyUserAdd

#Region "插入模式"

    Public Overrides Function DoAddCheck() As Boolean
        DoAddCheck = False
        If Me.txtUserName.Text.Trim = String.Empty Then
            ShowMessageBox("不能为空")
            Me.txtUserName.Focus()
            Exit Function
        End If
        DoAddCheck = True
    End Function

    Public Overrides Function DoAddExist() As Boolean
        Return True
    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        DoInsert = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" Insert Into ")
        sb.AppendLine(" WysInputUser ")
        sb.AppendLine(" (")
        sb.AppendLine(" UserName ")
        sb.AppendLine(" ,Memo ")
        sb.AppendLine(" ,OPTIME ")
        sb.AppendLine(" ,ID ")
        sb.AppendLine(" ,COLOR ")
        sb.AppendLine(" ,TYPE ")
        sb.AppendLine(" ) ")
        sb.AppendLine(" Values ")
        sb.AppendLine(" ( ")
        sb.AppendLine(" ? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        sb.AppendLine(" ,? ")
        If Me.rad内部.Checked Then
            sb.AppendLine(" ,0 ")
        ElseIf Me.rad借款人.Checked Then
            sb.AppendLine(" ,1 ")
        Else
            sb.AppendLine(" ,2 ")
        End If
        sb.AppendLine(" ) ")
        System.Guid.NewGuid.ToString("")
        Dim pa As ADODB.Parameter
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 8
        pa.Value = CMainModule.CmnToStr1(Me.txtUserName.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adChar, ADODB.ParameterDirectionEnum.adParamInput, 36, System.Guid.NewGuid.ToString("D"))
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Precision = 10
        pa.NumericScale = 5
        pa.Value = Me.btnColor.BackColor.ToArgb
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        cmd.CommandType = ADODB.CommandTypeEnum.adCmdText
        If ct.ExcCommand(cmd) Then
            Me.bAdd = True '表示成功插入数据能刷新界面        
            ShowMessageBox("插入成功")
            ClearForm()
            DoInsert = True
        Else
            DoInsert = False
        End If
    End Function

#End Region

#Region "清除窗体"
    Public Sub ClearForm()
        Me.txtUserName.Text = ""
        Me.txtMemo.Text = ""
    End Sub
#End Region

#Region "更新模式"

    Public Overrides Function DoUpdate() As Boolean
        MyBase.DoUpdate()
        DoUpdate = False
        Dim cmd As New ADODB.Command
        Dim sb As New StringBuilder
        sb.AppendLine(" UPDATE ")
        sb.AppendLine(" WysInputUser ")
        sb.AppendLine(" SET ")
        sb.AppendLine("  UserName=? ")
        sb.AppendLine(" ,Memo=? ")
        sb.AppendLine(" ,OPTIME=? ")
        sb.AppendLine(" ,COLOR=? ")
        sb.AppendLine("  WHERE  ")
        sb.AppendLine("  ID=? ")

        Dim pa As ADODB.Parameter
        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Size = 8
        pa.Value = CMainModule.CmnToStr1(Me.txtUserName.Text).Trim()
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.txtMemo.Text.Trim)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput)
        pa.Precision = 10
        pa.NumericScale = 5
        pa.Value = Me.btnColor.BackColor.ToArgb
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 36, CStr(Me.参数列表("ID")))
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()
        If (ct.ExcCommand(cmd)) Then
            ShowMessageBox("更新成功")
            bAdd = True
            Me.Close()
        Else
            ShowMessageBox("更新失败")
        End If
        DoUpdate = True
    End Function

    Public Overrides Function DoUpdateCheck() As Boolean
        MyBase.DoUpdateCheck()
        DoUpdateCheck = False
        If Me.txtUserName.Text.Trim = String.Empty Then
            ShowMessageBox("不能为空")
            Me.txtUserName.Focus()
            Exit Function
        End If
        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        DoUpdateExist = False
        MyBase.DoUpdateExist()
        If CStr(Me.参数列表("ID")).Trim = "" Then
            ShowMessageBox("没有选择任何记录")
            Exit Function
        End If
        DoUpdateExist = True
    End Function

    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()
        Me.Text = "用户更新"

        Me.GroupBox1.Enabled = False
        Dim rs As ADODB.Recordset = Nothing
        rs = ct.GetTable("select * from WysInputUser WHERE id='" + CStr(Me.参数列表("ID")) + "'")

        If rs IsNot Nothing Then
            If rs.EOF = False Then
                Me.txtUserName.Text = CMainModule.CmnToStr(rs.Fields("UserName").Value)
                Me.txtMemo.Text = CMainModule.CmnToStr(rs.Fields("Memo").Value)
                Select Case CInt(rs.Fields("type").Value)
                    Case 0
                        rad内部.Checked = True
                    Case 1
                        Me.rad借款人.Checked = True
                    Case 2
                        Me.rad贷款人.Checked = True
                End Select
                Me.btnColor.BackColor = System.Drawing.Color.FromArgb(CInt(rs.Fields("COLOR").Value))
                Exit Sub
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs)
        End If
        rs = Nothing
    End Sub

#End Region

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Me.ColorDialog1.Color = Me.btnColor.BackColor
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.btnColor.BackColor = Me.ColorDialog1.Color
        End If
    End Sub
End Class